export default {
	/**
	 * @param {Object} submitUrl 提交订单接口
	 * @param {Object} payData 支付接口
	 * @param {string} pay_type 支付方式 0 线上支付（微信支付）2货到付款 3 余额支付
	 * @param {Object} back_url 支付成功/失败返回页面
	 */
	submitOrder(submitUrl, payData, pay_type, back_url) {
		submitUrl.then(res => {
			payData({
				order_id: res.data.order_id,
				order_id_list: res.data.order_id_list,
				pay_type: pay_type
			}).then(res => {
				this.paySubmit(res, back_url)
			})
		})
	},
	/**
	 * 订单列表提交支付
	 * @param {Object} data 支付内容 order_id pay_type
	 * @param {Object} payData 支付接口
	 * @param {Object} back_url 支付成功/失败返回页面
	 */
	payOrder( data, payData, back_url) {
		payData({
			order_id: data.order_id,
			pay_type: data.pay_type
		}).then(res => {
			this.paySubmit(res, back_url)
		})
	},
	paySubmit (payData, back_url) {
		if (!payData.data) {
			uni.showToast({
			    title: payData.msg,
				icon: 'none',
			    duration: 2000
			});
			setTimeout(function() {
				uni.redirectTo({
				    url: back_url
				});
			}, 2000)
			return false;
		} else {
			uni.getProvider({
			    service: 'payment',
			    success: function (res) {
			        if (res.provider.indexOf('wxpay') > -1) {
			            uni.requestPayment({
							provider: 'wxpay',
							orderInfo: payData.res,
							timeStamp: payData.data.timeStamp,
							nonceStr: payData.data.nonceStr,
							package: payData.data.package,
							signType: payData.data.signType,
							paySign: payData.data.paySign,
							success: function (res) { },
							complete: function (res) {
								if ("requestPayment:fail" != res.errMsg && "requestPayment:fail cancel" != res.errMsg) {
									uni.showToast({
									    title: '支付成功',
										icon: 'none',
									    duration: 2000
									});
								} else {
									uni.showToast({
									    title: '订单尚未支付',
										icon: 'none',
									    duration: 2000
									});
								}
								setTimeout(function() {
									uni.redirectTo({
									    url: back_url
									});
								}, 2000)
								return true;
							}
						})
			        }
			    }
			});
		}
	}
}
